package com.pheruagor.handmanager.model {
    import com.pheruagor.handmanager.model.business.Member;
    import com.pheruagor.handmanager.model.business.Person;
    import com.pheruagor.handmanager.model.business.activity.PlayerActivity;

    import mx.collections.ArrayCollection;
    import mx.utils.UIDUtil;

    import org.puremvc.as3.patterns.proxy.Proxy;

    public class MemberEditionPopUpProxy extends Proxy {
        public static const NAME:String = "memberEditionPopUpProxy";
        //-----------------------------------------------------
        // Properties
        //-----------------------------------------------------
        private var m_genderList:ArrayCollection;
        private var m_positionList:ArrayCollection;
        private var m_handList:ArrayCollection;

        private var m_member:Member;

        //-----------------------------------------------------
        // Initialization
        //-----------------------------------------------------
        public function MemberEditionPopUpProxy(member:Member = null) {
            super(NAME, member);
            if (member == null) {
                m_member = new Member();
                m_member.id = UIDUtil.createUID();
            } else {
                m_member = Member(member).clone()
            }
            initializeMember();
        }

        //-----------------------------------------------------
        // IProxy implementation
        //-----------------------------------------------------
        override public function onRegister():void {

        }

        override public function onRemove():void {

        }

        //-----------------------------------------------------
        // Access
        //-----------------------------------------------------
        public function get member():Member {
            return m_member;
        }

        public function get genderList():ArrayCollection {
            if (m_genderList == null) {
                m_genderList = new ArrayCollection([{"label": "Homme", "value": Person.MALE}, {"label": "Femme", "value": Person.FEMALE}]);
            }
            return m_genderList;
        }

        public function get handList():ArrayCollection {
            if (m_handList == null) {
                m_handList = new ArrayCollection([{"label": "Non renseigné", "value": null}, {"label": "Gaucher"
                        , "value": Person.GENETIC_HAND_LEFT}, {"label": "Droitier", "value": Person.GENETIC_HAND_RIGHT}
                    , {"label": "Ambidextre", "value": Person.GENETIC_HAND_BOTH}]);
            }
            return m_handList;
        }

        public function set forname(value:String):void {
            m_member.forname = value;
        }

        public function set lastname(value:String):void {
            m_member.lastname = value;
        }

        public function set size(value:Number):void {
            m_member.size = value;
        }

        public function set gender(value:String):void {
            m_member.gender = value;
        }

        public function set geneticHand(value:String):void {
            m_member.geneticHand = value;
        }

        public function set birthday(value:Date):void {
            m_member.birthday = value;
        }

        public function set subscriptionDate(value:Date):void {
            m_member.clubEntryDate = value;
        }

        public function set avatarPath(value:String):void {
            m_member.avatarPath = value;
        }

        public function set phoneNumber(value:String):void {
            m_member.phoneNumber = value;
        }

        public function get playerActivity():PlayerActivity {
            if (m_member.activities != null && m_member.activities.length > 0) {
                return PlayerActivity(m_member.activities.getItemAt(0));
            }
            var playerActivity:PlayerActivity = new PlayerActivity();
            playerActivity.id = UIDUtil.createUID();
            playerActivity.beginDate = new Date();
            playerActivity.memberId = m_member.id;
            m_member.activities.addItem(playerActivity);

            return playerActivity;
        }


        private function initializeMember():void {
            if (m_member.activities == null) {
                m_member.activities = new ArrayCollection();
            }
            if (m_member.gender == null) {
                m_member.gender = Person.MALE;
            }
        }
    }
}